﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO
{
    public partial class Output_Indicators : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void AddOutputIndicatorButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Edit/AddOutputIndicator.aspx", true);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        Button button = control as Button;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('Are you sure " +
                                   "you want to delete this record?')) return;";
                    }
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            Button deleteButton = (Button)(sender);
            int indicatorId = int.Parse(deleteButton.CommandArgument.Trim());
            try
            {
                indicator delIndicator = db.indicators.Where(data => data.indicator_id == indicatorId).Single();

                var _delIndicatorResult = from data in db.indicator_result
                                         where data.indicator_id == delIndicator.indicator_id
                                         select data;
                foreach (indicator_result delIndicatorResult in _delIndicatorResult)
                {
                    db.indicator_result.Remove(delIndicatorResult);
                }

                var _delMonitoringPlanIndicator = db.activity_indicator_connector.Where(data => data.indicator_id == delIndicator.indicator_id);
                foreach (activity_indicator_connector delMonitoringPlanIndicator in _delMonitoringPlanIndicator)
                {
                    db.activity_indicator_connector.Remove(delMonitoringPlanIndicator);
                }

                db.indicators.Remove(delIndicator);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                db.SaveChanges();
                GridView1.DataBind();
                GridView2.DataBind();
            }
        }
    }
}